/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.xy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xy.constants.RedisConstant;
import com.xy.entity.BlacklistEntity;
import com.xy.entity.ExhibitionEntity;
import com.xy.entity.ViewerEntity;
import com.xy.enums.BlacklistTypeEnum;
import com.xy.enums.CardTypeEnum;
import com.xy.service.IExhibitionService;
import com.xy.service.IViewerService;
import com.xy.vo.BlacklistVO;
import com.xy.excel.BlacklistExcel;
import com.xy.mapper.BlacklistMapper;
import com.xy.service.IBlacklistService;
import lombok.AllArgsConstructor;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * 黑名单 服务实现类
 *
 * @author BladeX
 * @since 2023-07-18
 */
@Service
@AllArgsConstructor
public class BlacklistServiceImpl extends BaseServiceImpl<BlacklistMapper, BlacklistEntity> implements IBlacklistService {

	private final IViewerService viewerService;
	private final IExhibitionService exhibitionService;
	private final BladeRedis bladeRedis;

	@Override
	public IPage<BlacklistVO> selectBlacklistPage(IPage<BlacklistVO> page, BlacklistVO blacklist) {
		return page.setRecords(baseMapper.selectBlacklistPage(page, blacklist));
	}


	@Override
	public List<BlacklistExcel> exportBlacklist(Wrapper<BlacklistEntity> queryWrapper) {
		List<BlacklistExcel> blacklistList = baseMapper.exportBlacklist(queryWrapper);
		//blacklistList.forEach(blacklist -> {
		//	blacklist.setTypeName(DictCache.getValue(DictEnum.YES_NO, Blacklist.getType()));
		//});
		return blacklistList;
	}


	/**
	 * 黑名单导入
	 *
	 * @param entityList
	 */
	@Override
	public void importBlacklis(List<BlacklistEntity> entityList) {
		for (BlacklistEntity blacklistEntity : entityList) {
			blacklistEntity.setIdCard(replaceBlank(blacklistEntity.getIdCard()));
			BlacklistEntity blacklist = getBlacklist(blacklistEntity.getIdCard());
			if (blacklist == null) {
				save(blacklistEntity);
			} else {
				blacklistEntity.setId(blacklist.getId());
				blacklistEntity.setId(blacklist.getId());
				updateById(blacklistEntity);
			}
			String key = RedisConstant.LIMITED_ACCESS.concat(Func.toStr(blacklist.getExhibitionId())).concat(blacklistEntity.getIdCard());
			bladeRedis.set(key, blacklistEntity);
		}

	}

	public static String replaceBlank(String str) {
		String dest = "";
		if (str != null) {
			//空格\t、回车\n、换行符\r、制表符\t
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
		}
		return dest;
	}

	/**
	 * 黑名单新增
	 *
	 * @param entityList
	 */
	@Override
	public void saveBlackList(List<ViewerEntity> entityList) {
		for (ViewerEntity viewer : entityList) {
			BlacklistEntity blacklistEntity = BeanUtil.copy(viewer, BlacklistEntity.class);
			if (blacklistEntity == null) {
				continue;
			}
			BlacklistEntity blacklist = getBlacklist(viewer.getIdCard());
			if (blacklist == null) {
				blacklistEntity.setCardType(CardTypeEnum.SFZ.getCode());
				blacklistEntity.setBlacklistType(BlacklistTypeEnum.ZB.getCode());
				save(blacklistEntity);
			} else {
				blacklistEntity.setId(blacklist.getId());
				updateById(blacklistEntity);
			}
			String key = RedisConstant.LIMITED_ACCESS.concat(Func.toStr(blacklistEntity.getExhibitionId())).concat(blacklistEntity.getIdCard());
			bladeRedis.set(key, blacklistEntity);
		}
	}

	@Override
	public void saveOrUpdateData(BlacklistEntity blacklist) {
		if (blacklist.getId() != null) {
			BlacklistEntity entity = this.getById(blacklist.getId());
			if (entity != null) {
				this.updateById(entity);
				String key = RedisConstant.LIMITED_ACCESS.concat(Func.toStr(entity.getExhibitionId())).concat(entity.getIdCard());
				bladeRedis.del(key);
			}
		}
		this.saveOrUpdate(blacklist);
		String key = RedisConstant.LIMITED_ACCESS.concat(Func.toStr(blacklist.getExhibitionId())).concat(blacklist.getIdCard());
		bladeRedis.set(key, blacklist);
	}

	@Override
	public void batchSave(List<BlacklistEntity> blacklistEntities) {
		for (BlacklistEntity blacklistEntity : blacklistEntities) {
			blacklistEntity.setCardType(CardTypeEnum.SFZ.getCode());
			blacklistEntity.setBlacklistType(BlacklistTypeEnum.ZB.getCode());
			BlacklistEntity blacklist = getBlacklist(blacklistEntity.getIdCard());
			if (blacklist == null) {
				save(blacklistEntity);
			} else {
				blacklistEntity.setId(blacklist.getId());
				updateById(blacklistEntity);
			}
			if(Func.isEmpty(blacklistEntity.getIdCard())){

				String key = RedisConstant.LIMITED_ACCESS.concat(Func.toStr(blacklist.getExhibitionId())).concat(blacklistEntity.getIdCard());
				bladeRedis.set(key, blacklistEntity);
			}

		}
	}

	/**
	 * 缓存黑名单
	 *
	 * @param blackList
	 */
	public void cashBlack(List<BlacklistEntity> blackList) {
		blackList.forEach(o -> {
			String key = RedisConstant.LIMITED_ACCESS.concat(Func.toStr(o.getExhibitionId())).concat(o.getIdCard());
			bladeRedis.set(key, o);
		});
	}

	/**
	 * 查询票证
	 *
	 * @param collect
	 * @return
	 */
	private List<ViewerEntity> queryLisViewer(List<String> collect) {
		//查询开展中的展会
		List<ExhibitionEntity> exhibitionEntityList = exhibitionService.queryExhibition();
		List<Long> longList = exhibitionEntityList.stream().map(ExhibitionEntity::getId).collect(Collectors.toList());
		//查询用户
		LambdaQueryWrapper<ViewerEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.in(ViewerEntity::getIdCard, collect);
		queryWrapper.in(ViewerEntity::getExhibitionId, longList);
		return viewerService.list(queryWrapper);

	}

	/**
	 * 查询票证
	 *
	 * @param idCard
	 * @return
	 */
	@Override
	public BlacklistEntity getBlacklist(String idCard) {
		LambdaQueryWrapper<BlacklistEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(BlacklistEntity::getIdCard, idCard);
		List<BlacklistEntity> list = this.list(queryWrapper);
		if (CollectionUtil.isNotEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

}
